<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>
<%@page import="javazoom.upload.*"%> 
<%@page import="uploadutilities.FileMover"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="jxl.Cell"%>
<%@page import="jxl.Sheet"%>
<%@page import="jxl.Workbook"%>
<%@page import="jxl.WorkbookSettings"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@page import="db.Place"%>
<%@page import="db.PlaceDAO"%>

<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%@ include file="/jsp/common/utility/ImportExecl.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	PlaceDAO placeDAO = (PlaceDAO)ctx.getBean("PlaceDAO");
	FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
	UploadBean upBean = new UploadBean();
	int tid = (Integer)session.getAttribute("tid");
	MultipartFormDataRequest mrequest = null;
	Hashtable files = null;
	boolean success = true;
	String message = "";
	String serverPath = new String();
	
	if (MultipartFormDataRequest.isMultipartFormData(request))
	{
		mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"utf-8");//注意这里也要设置编码参数
		//这里用来测试title参数是否正确。调试的时候，加一句if (true)return;即可。
		files = mrequest.getFiles();
	}
	
	
	//获取修改前的文件名
	String sOldFileName =mrequest.getParameter("oldfilename"); 
	//out.println("sOldFileName:"+sOldFileName);
	String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
	String sPath=sWebRootPath+"allStockTemp";
	int iFileCount = 0;
	String sServerFileName="";
	String sLocalFileName = "";
	//文件获取
	if ( (files != null) || (!files.isEmpty()) ) {
		iFileCount = files.size();
		UploadFile file = (UploadFile) files.get("attach");
		sLocalFileName=file.getFileName();
		out.println("sLocalFileName:"+sLocalFileName);
		int ii= sLocalFileName.indexOf("."); //取文件名的后缀
		String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
		//得到不重复的文件名
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= fmt.format(dt);
		sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		upBean.setFolderstore(sPath);//设置要上传的目录
		upBean.addUploadListener(fileMover);//增加filMover监听
		fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
		upBean.store(mrequest, "attach");//上传
		serverPath = sPath+"/"+sServerFileName;
		//System.out.println(serverPath);
	}else{
		success = false;
		message = "文件上传失败。";
	}
	List listjxl = new ArrayList();
	if(serverPath!=null){
		listjxl = getListByJxl(serverPath,success,message);
		request.setAttribute("serverPath",serverPath);
	}
	List listmap = new ArrayList();
	if(listjxl!=null && listjxl.size()>0){
		for(int i=0;i<listjxl.size();i++){
			List li = (List)listjxl.get(i);
			//System.out.println("===========>>>>>"+li.size());
			Map map = new HashMap();
			if(li!=null && li.size()>=11){
				
				map.put("storehouse",li.get(0));
				///如果库位不为null那么建立库位
				if(li.get(0)!=null && !"".equals(li.get(0))){
					//建立库位信息
					List plalist = placeDAO.findByName(li.get(0));
					if(plalist!=null && plalist.size()>0){
						//库位存在
						
					}else{
						//不存在
						Place place = new Place();
						place.setName((String)li.get(0));
						placeDAO.save(place);
					}
					
				}				
				
				
				
				
				
				
				
				map.put("supplier",li.get(1));
				List matlist = materialDAO.findByCode(li.get(2));
				//System.out.println("##########"+li.get(2));
				if(matlist!=null && matlist.size()>0){
					for(int m=0;m<matlist.size();m++){
						Material material = (Material)matlist.get(m);
						if(material.getIsDelete()==0 && material.getTeamId()==tid){
							map.put("materialcodebo",true);
							map.put("matcode",li.get(2));
							map.put("matname",li.get(3));
							map.put("matcom",li.get(7));
						}else{
							//系统自动建立
							Material newmaterial = new Material();
							newmaterial.setCode((String)li.get(2));
							newmaterial.setCompany((String)li.get(7));
							newmaterial.setIsDelete(0);
							newmaterial.setDate(new Date());
							newmaterial.setName((String)li.get(3));
							newmaterial.setTeamId(tid);
							materialDAO.save(newmaterial);
							map.put("materialcodebo",true);
							map.put("matcode",newmaterial.getCode());
							map.put("matname",newmaterial.getName());
							map.put("matcom",newmaterial.getCompany());
						}
					}
					
				}else{
					//系统自动建立
							Material newmaterial = new Material();
							newmaterial.setCode((String)li.get(2));
							newmaterial.setCompany((String)li.get(7));
							newmaterial.setIsDelete(0);
							newmaterial.setDate(new Date());
							newmaterial.setName((String)li.get(3));
							newmaterial.setTeamId(tid);
							materialDAO.save(newmaterial);
							map.put("materialcodebo",true);
							map.put("matcode",newmaterial.getCode());
							map.put("matname",newmaterial.getName());
							map.put("matcom",newmaterial.getCompany());
				}
				
				if(li.get(4)!=null && !"".equals(li.get(4))){
					String zk = (String)li.get(4);
					if(zk.indexOf("通用")!=-1 || zk.indexOf("专项")!=-1){
						map.put("zikubo",true);//子库
						map.put("ziku",li.get(4));//子库
					}else{
						map.put("zikubo",false);
						success = false;
						message = "子库信息错误。";
					}
				}
				
				
				
				
				
				
				map.put("procode",li.get(5));
				
				//项目名称
				if(li.get(5)!=null && (li.get(5)!="" || !"".equals(li.get(5)))){
					String pr = (String)li.get(5);
					//System.out.println("111111111111111111========>>>>>>>+"+pr);
					if(pr.lastIndexOf("|")!=-1){//找到结合货位例如  A001专项|Mjb10ca00200|   则进行字符截取。只取中间的项目编码。
						String[] strs = pr.split("\\|");
						pr = strs[1];
					}
					//System.out.println("22222222222222222========>>>>>>>+"+pr);
					List listpro = projectDAO.findByCode(pr);
					if(listpro!=null && listpro.size()>0){
						for(int p=0;p<listpro.size();p++){
							Project project = (Project)listpro.get(p);
							if(project.getIsDelete()==0){//有效
								map.put("procodebo",true);
								map.put("pronamebo",true);
							}else{
								Project newproject = new Project();
								newproject.setDate(new Date());
								newproject.setCode(pr);
								newproject.setIsDelete(0);
								newproject.setName(pr);
								newproject.setTeamId(tid);
								newproject.setManager("");
								newproject.setSubCode("");
								newproject.setSubName("");
								newproject.setSubstoreId(1);
								projectDAO.save(newproject);
								map.put("procodebo",true);
								map.put("pronamebo",true);
								message = "项目信息在系统中不存在,请在系统中建立项目信息。";
							}
						}
						
					}else{
						Project newproject = new Project();
						newproject.setDate(new Date());
						newproject.setCode(pr);
						newproject.setIsDelete(0);
						newproject.setName(pr);
						newproject.setTeamId(tid);
						newproject.setManager("");
						newproject.setSubCode("");
						newproject.setSubName("");
						newproject.setSubstoreId(1);
						projectDAO.save(newproject);
						map.put("procodebo",true);
						map.put("pronamebo",true);
						message = "项目信息在系统中不存在,请在系统中建立项目信息。";
					}
				}else{
					map.put("procodebo",true);
					map.put("pronamebo",true);
				}
				
				
				
				
				map.put("batch",li.get(6));
				map.put("purprice",li.get(8));
				map.put("useNum",li.get(9));
				map.put("importNum",li.get(10));
				if(li.get(9)!=null && li.get(10)!=null && !"".equals((String)li.get(9)) && !"".equals((String)li.get(10))){
					if(li.get(10).equals(li.get(9))){
						map.put("importNumbo",true);
						map.put("importNumbo",true);
					}else{
						map.put("importNumbo",false);
						map.put("importNumbo",false);
						success = false;
						message = "可用数量于库存数量不匹配。";
					}
				}
				
				
				
				//System.out.println("===========storehousestorehousestorehouse======"+li.get(1));
				
			}else{
				success = false;
				message = "解析出错，execl文件格式行数不够，请仔细检查execl文件。";
			}
			listmap.add(map);
		}
	}
	//System.out.println("listjxl=================================================>"+listmap.size()+"==================>"+listjxl.size());
	request.setAttribute("listjxl",listmap);
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/stock/allStock.vm");
%>